package cn.amos.web.controller;

import cn.amos.core.business.UserBusiness;
import cn.amos.dao.entity.UserEntity;
import cn.amos.facade.request.UserForm;
import cn.amos.web.request.UserForgetPwdRequest;
import cn.amos.web.request.UserLoginRequest;
import cn.amos.web.request.UserRegisterRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户相关API
 *
 * @author DaoyuanWang
 */
@RequestMapping(value = "user")
@Controller
public class UserController {

    @Resource
    private UserBusiness userBusiness;

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    /**
     * 跳转到登录界面
     */
    @RequestMapping(value = "login.do", method = RequestMethod.GET)
    public String userLogin() {
        return "back/UserLogin";
    }

    /**
     * 跳转到登录界面
     */
    @RequestMapping(value = "product.do", method = RequestMethod.GET)
    public String product(HttpServletRequest request) {
        String token = request.getParameter("user_login");
        LOGGER.info("上一步传的文字:[{}]", token);
        request.setAttribute("user_login", token);
        return "back/ProductPage";
    }

    /**
     * ajax请求-检查用户是否存在
     */
    @RequestMapping(value = "existUser.do")
    @ResponseBody
    public boolean existUser(String name) {
        return userBusiness.existUser(name);
    }

    /**
     * 用户登录
     */
    @RequestMapping(value = "loginForm.do", method = RequestMethod.POST)
    public ModelAndView checkLogin(@ModelAttribute UserLoginRequest userLoginRequest, HttpServletResponse response) {

        ModelAndView modelAndView = new ModelAndView();
        UserForm userForm = new UserForm();
        userForm.setPhone(userLoginRequest.getPhoneLogin());

        UserEntity userEntity = userBusiness.login(userForm);

        if (userEntity != null) {
            String text = userEntity.getName() != null ? userEntity.getName() : userEntity.getPhone();
            if (userEntity.getPwd().equals(userLoginRequest.getPwdLogin())) {
                modelAndView.addObject("user_login", text);
                modelAndView.setViewName("redirect:product.do");
            } else {
                modelAndView.addObject("user_login", "您的密码输入错误,请重试");
                modelAndView.setViewName("back/UserLogin");
            }
        } else {
            modelAndView.addObject("user_login", "您还未注册,请先注册哈");
            modelAndView.setViewName("back/UserLogin");
        }

        return modelAndView;
    }

    /**
     * 用户注册
     */
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    public ModelAndView register(@ModelAttribute UserRegisterRequest registerRequest) {
        UserForm userForm = new UserForm();
        userForm.setName(registerRequest.getNameRegister());
        userForm.setPhone(registerRequest.getPhoneRegister());
        userForm.setPwd(registerRequest.getPwdRegister());

        String register = userBusiness.register(userForm);
        if (register == null) {
            return new ModelAndView("back/UserLogin", "user_login", "注册成功");
        }

        return new ModelAndView("back/UserLogin", "user_login", register);
    }

    /**
     * 用户忘记密码
     */
    @RequestMapping(value = "forgetPassword.do", method = RequestMethod.POST)
    public ModelAndView forgetPassword(@ModelAttribute UserForgetPwdRequest forgetPwdRequest) {
        String phoneNo = forgetPwdRequest.getPhoneForgetPwd();
        UserForm userForm = new UserForm();
        userForm.setName(forgetPwdRequest.getNameForgetPwd());
        userForm.setPhone(phoneNo);

        if (StringUtils.isBlank(phoneNo)) {
//            return new ModelAndView("redirect:login.do", "user_login", "请先输入手机号");
            return new ModelAndView("back/UserLogin", "user_login", "请先输入手机号");
        }

        String updatePwd = userBusiness.updatePwd(userForm);
        if (updatePwd == null) {
            return new ModelAndView("back/UserLogin", "user_login", "密码修改成功,请重新登录");
        }

        return new ModelAndView("back/UserLogin", "user_login", updatePwd);
    }

    /**
     * 修改用户信息
     */
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    @ResponseBody
    public void update(@RequestBody UserLoginRequest userLoginRequest) {
        UserForm userForm = new UserForm();
        BeanUtils.copyProperties(userLoginRequest, userForm);

        userBusiness.update(userForm);
    }

    /**
     * 修改用户密码
     */
    @RequestMapping(value = "updatePwd.do", method = RequestMethod.POST)
    @ResponseBody
    public void updatePwd(@RequestBody UserForgetPwdRequest userForgetPwdRequest) {
        UserForm userForm = new UserForm();
        userForm.setName(userForgetPwdRequest.getNameForgetPwd());
        userForm.setPhone(userForgetPwdRequest.getPhoneForgetPwd());
        userForm.setPwd(userForgetPwdRequest.getPwdForgetPwd());
        userBusiness.updatePwd(userForm);
    }

}
